// pages/parkingSpace/parkingSpace.js
import {
    parkingSpace
} from '../../api/api'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        current:1,
        goodList:[],
        reachFalg:false,//是否可以上拉
    },
    //点击事件
    handleClick(e) {
        let url = '/pages/details/details?id=' + e.currentTarget.dataset.id

        wx.navigateTo({
            url,
        })
    },
     //图片加载失败时
     errorFunction: function (event) {
        console.log(event);
        var index = event.currentTarget.dataset.index
        var img = 'goodList[' + index + '].img'
        this.setData({
            [img]: '../../static/u610.png'
        })
    },
    //加载数据
    parkingSpaces() {
        let premse = {
            current: this.data.current,
            pageSize: 10,
            "param": {
                "isFree": 1
            },
        }
        parkingSpace(premse).then(res => {
            let datas = res.data || {};
            let goodList = this.data.goodList || []
            if(this.data.current == 1)goodList=[];
            let records = datas.records || [];
            let reachFalg = false
            if(records.length > 0){//说明还有下一页
                reachFalg = true
            }
            goodList = goodList.concat(records)
            this.setData({
                goodList,
                reachFalg
            })
            wx.stopPullDownRefresh()//关闭下拉
            console.log(this.data.goodList);
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.setData({
            current:1
        })
        this.parkingSpaces()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.setData({
            current:1
        })
        this.parkingSpaces()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if(this.data.reachFalg){
            let current = this.data.current
            current++
            this.setData({
                current
            })
            this.parkingSpaces()
        }
        console.log(1);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})